package cn.com.pajx.pajx_visitor_v3.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.rcw.swiperefreshrecyclerview.BaseAdapter;
import com.rcw.swiperefreshrecyclerview.ViewHolder;

import java.util.List;

import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.bean.KeyboardBean;

/**
 * 数字键盘的Adapter
 */
public class KeyboardAdapter extends BaseAdapter<KeyboardBean> {

    private int selection = -1;

    public KeyboardAdapter(Context context, int layoutId, List<KeyboardBean> keyboardBeans) {
        super(context, layoutId, keyboardBeans);
    }

    @Override
    public void convert(ViewHolder holder, KeyboardBean keyboardBean, int position) {
        TextView tvKeyboard = holder.getView(R.id.view_key);
        ImageView ivDelete = holder.getView(R.id.iv_delete);
        tvKeyboard.setText(keyboardBean.getKey());
        ivDelete.setVisibility(View.INVISIBLE);
        tvKeyboard.setVisibility(View.VISIBLE);
        switch (keyboardBean.getKey_code()) {
            case "0"://数字
                changeViewStatus(holder, position);
                break;
            case "1"://删除
                ivDelete.setVisibility(View.VISIBLE);
                tvKeyboard.setVisibility(View.INVISIBLE);
                changeViewStatus(holder, position);
                break;
            case "2"://查询
                holder.itemView.setBackgroundResource(R.drawable.shape_confirm_gradient);
                break;
        }
    }

    private void changeViewStatus(ViewHolder holder, int position) {
        if (selection == position) {
            holder.itemView.setBackgroundResource(R.drawable.shape_keyboard_content_bg);
        } else {
            holder.itemView.setBackgroundResource(R.drawable.shape_common_gradient);
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setSelection(int selection) {
        this.selection = selection;
        notifyDataSetChanged();
    }
}
